﻿using Kanban.BLLContainer;
using Kanban.IBLL;
using Kanban.Model;
using KanbanApi.Models;
using KanbanApi.Models.Auth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace KanbanApi.Controllers
{
    public class BoardController : ApiController
    {
        private readonly IBoardService BoardService = Container.Resolve<IBoardService>();

        [HttpGet]
        [Route("api/Board/GetNum")]
        public Task<object> GetNum()
        {
            return BoardService.GetNum();
        }

        [HttpGet]
        [Route("api/Board/GetSystemInfo")]
        public Task<object> GetSystemInfo()
        {
            return BoardService.GetSystemInfo();
        }

        [HttpGet]
        [Route("api/Board/GetSystemInfos")]
        public async Task<object> GetSystemInfos()
        {
            var response = await BoardService.GetSystemInfos();
            return new { systemData = response.Item1, noticeData = response.Item2 };
        }
        

        [HttpGet]
        [Route("api/Board/GetRacks")]
        public Task<IEnumerable<object>> GetRacks()
        {
            return BoardService.GetRacks();
        }

        [HttpGet]
        [Route("api/Board/GetInPageData")]
        public async Task<Table<object>> GetInPageData(string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, string startTime, string endTime, int pageSize, int pageIndex, bool Qty = false)
        {
            var response = await BoardService.GetInPageData(PKGID, Location,  StoreCode,  SHELFTNO, PN, startTime, endTime, pageSize, pageIndex, Qty);
            return new Table<object>(response.Item1, response.Item2);
        }

        [HttpGet]
        [Route("api/Board/GetOutPageData")]
        public async Task<Table<object>> GetOutPageData(string TaskNo, string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, string startTime, string endTime, int pageSize, int pageIndex)
        {
            var response = await BoardService.GetOutPageData(TaskNo, PKGID, Location,  StoreCode,  SHELFTNO, PN, startTime, endTime, pageSize, pageIndex);
            return new Table<object>(response.Item1, response.Item2);
        }

        [HttpGet]
        [Route("api/Board/GetTaskPageData")]
        public async Task<Table<object>> GetTaskPageData(string TaskNo, string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, int pageSize, int pageIndex)
        {
            var response = await BoardService.GetTaskPageData(TaskNo, PKGID, Location,  StoreCode,  SHELFTNO, PN, pageSize, pageIndex);
            return new Table<object>(response.Item1, response.Item2);
        }

        [HttpGet]
        [Route("api/Board/GetTaskCancelPageData")]
        public async Task<Table<object>> GetTaskCancelPageData(string search, int pageSize, int pageIndex)
        {
            var response = await BoardService.GetTaskCancelPageData(search, pageSize, pageIndex);
            return new Table<object>(response.Item1, response.Item2);
        }

        [HttpGet]
        [Route("api/Board/GetWarehousePageData")]
        public async Task<Table<object>> GetWarehousePageData(string PKGID, string Location, string StoreCode, string SHELFTNO, string PN, int pageSize, int pageIndex)
        {
            var response = await BoardService.GetWarehousePageData(PKGID, Location,  StoreCode,  SHELFTNO, PN, pageSize, pageIndex);
            return new Table<object>(response.Item1, response.Item2);
        }

        [HttpGet]
        [Route("api/Board/GetSysStatus")]
        public Task<IEnumerable<object>> GetSysStatus()
        {
            return BoardService.GetSysStatus();
        }

        [HttpPost]
        [Route("api/Board/UpdateRunStatus")]
        public Task<bool> UpdateRunStatus(bool status)
        {
            return BoardService.UpdateRunStatus(status);
        }

        [HttpPost]
        [Route("api/Board/DelTask")]
        public async Task<LoginResult> DelTask(string taskNo)
        {
            var response = await BoardService.DelTask(taskNo);
            return new LoginResult { Success = response.Item1, Message = response.Item2 };
        }

        [HttpPost]
        [Route("api/Board/DelWarehouse")]
        public async Task<LoginResult> DelWarehouse(string serialNo)
        {
            var response = await BoardService.DelWarehouse(serialNo);
            return new LoginResult { Success = response.Item1, Message = response.Item2 };
        }

        [HttpGet]
        [Route("api/Board/GetApiLogPageData")]
        public async Task<Table<object>> GetApiLogPageData(string time, int pageSize, int pageIndex)
        {
            var response = await BoardService.GetApiLogPageData(time, pageSize, pageIndex);
            return new Table<object>(response.Item1, response.Item2);
        }

        [HttpGet]
        [Route("api/Board/GetLogPageData")]
        public async Task<Table<object>> GetLogPageData(string time, int pageSize, int pageIndex)
        {
            var response = await BoardService.GetLogPageData(time, pageSize, pageIndex);
            return new Table<object>(response.Item1, response.Item2);
        }

        [HttpPost]
        [Route("api/Board/BatchCompleteTask")]
        public async Task<LoginResult> BatchCompleteTask(string ids)
        {
            var response = await BoardService.BatchCompleteTask(ids);
            return new LoginResult { Success = response };
        }

        [HttpPost]
        [Route("api/Board/GetWarehouseWarnList")]
        public Task<IEnumerable<object>> GetWarehouseWarnList()
        {
            return BoardService.GetWarehouseWarnList();
        }

        [HttpGet]
        [Route("api/Board/GetBoardData")]
        public async Task<object> GetBoardData()
        {
            var response = await BoardService.GetBoardData();
            return new { num = response.Item1, inData = response.Item2, outData = response.Item3, warnData = response.Item4, taskData = response.Item5 };
        }
    }
}